<template>

	<view class="flowLaunch-v">

		<!-- 搜索 -->
		<view class="search-box search-box_sticky">
			<u-search placeholder="请输入检验编号搜索" v-model="keyword" height="72" :show-action="false" @change="search"
				bg-color="#f0f2f6" shape="square">
			</u-search>
		</view>

		<!-- 列表 -->
		<mescroll-body ref="mescrollRef" @init="mescrollInit"  @down="downCallback" @up="upCallback" :sticky="true" :down="downOption"
			:up="upOption">

			<view class="flow-list" v-if="list.length > 0" style="padding-top: 5px;padding-bottom: 5px;">
				<view class="flow-list-box">
					<uni-swipe-action ref="swipeAction">
						<uni-swipe-action-item v-for="(item, index) in list" :key="item.id" :threshold="0"
							:right-options="options" @click="handleClick(index)" class="data-item">
							<view class="item" @click="goDetail(item)" :id="'item'+index" ref="mydom"
								style="height: 200px;">
								<view class="item-left">
									<view class="item-left-top">
										<text class="title u-line-1 u-font-24">检验批号 :<text
												class="titInner">{{item.code ? item.code : ''}}</text></text>

										<u-tag v-if="item.overdue == 2 " text="逾期" shape="square" type="error"
											class="tag-class" />
										<u-tag v-else-if="item.overdue == 1 " text="即将逾期" shape="square" type="warning "
											class="tag-class" />
										<u-tag v-else text="正常" shape="square" type="success" class="tag-class" />

									</view>

									<text class="title u-line-1 u-font-24">物&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;料 :<text
											class="titInner">{{item.materialCode ? item.materialCode : ''}}</text>
										<text class="titInner">{{item.materialDes ? item.materialDes : ''}}
										</text></text>
									<text class="title u-line-1 u-font-24">供&nbsp;应&nbsp;商&nbsp; :<text
											class="titInner">{{item.supplier ? item.supplier : ''}}</text>
										<text class="titInner">
											{{item.supplierDes ? item.supplierDes : ''}}</text></text>
									<text class="title u-line-1 u-font-24">批&nbsp;次&nbsp;号&nbsp; :<text
											class="titInner">{{item.batchNumber ? item.batchNumber : ''}}</text></text>
									<text class="title u-line-1 u-font-24">报检类型 :<text
											class="titInner">{{item.type ? item.type : ''}}</text></text>
									<text class="title u-line-1 u-font-24">来料数量 :<text
											class="titInner">{{item.incomingQty ? item.incomingQty : ''}}</text></text>
									<text class="title u-line-1 u-font-24">检&nbsp;验&nbsp;员&nbsp; :<text
											class="titInner">{{item.employeeName ? item.employeeName : ''}}</text></text>
									<text class="time title u-font-24">检验日期 :<text
											class="titInner">{{item.date | date('yyyy-mm-dd hh:MM')}}</text></text>
								</view>
								<view class="item-right">
									<image :src="item.flowStatus" mode="widthFix" class="item-right-img">
									</image>
								</view>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>
<script>
	import resources from '@/libs/resources.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {
		inspectionlot,
		Delete
	} from '@/api/q1/business/inspection/inspection.js'
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				flowStatus: '',
				downOption: {
					use: true,
					auto: true
				},
				upOption: {
					page: {
						num: 0,
						size: 10,
						time: null
					},
					empty: {
						use: true,
						icon: resources.message.nodata,
						tip: "暂无数据",
						fixed: true,
						top: "300rpx",
					},
					textNoMore: '没有更多数据',
				},
				keyword: '',
				list: [],
				options: [
					// 	{
					// 	text: '删除',
					// 	style: {
					// 		backgroundColor: '#dd524d'
					// 	}
					// }
				]
			}
		},
		onShow(e) {

			uni.$on('refresh', () => {
				this.list = [];
				this.mescroll.resetUpScroll();
			})

			// this.upCallback({
			// 	num: 1,
			// 	size: 10
			// })

		},
		onUnload() {

			uni.$off('refresh')
		},
		methods: {
			//获取上个页面数据
			getValue(result) {

				if (result == 'success') {

					this.upCallback({
						num: 1,
						size: 10
					})
				}
			},
			//刷新
			upCallback(page) {
				let query = {
					currentPage: page.num,
					pageSize: page.size,
					statusType: "Wait", //未检验
					code: this.keyword
				}
				inspectionlot(query, {
					load: page.num == 1
				}).then(res => {

					this.mescroll.endSuccess(res.data.list.length);
					if (page.num == 1) this.list = [];
					const list = res.data.list.map(o => ({
						'flowStatus': this.getFlowStatus(o.status),
						...o
					}))
					this.list = this.list.concat(list);
				}).catch(() => {
					this.mescroll.endErr();
				})
			},

			getFlowStatus(status) {
				let flowStatus;
				switch (status) {
					case 0: //未检验
						flowStatus = resources.status.unchecked
						break;
					case 1: //检验中
						flowStatus = resources.status.check
						break;
					case 2: //已检验
						flowStatus = resources.status.success
						break;
					case 3: //已过帐
						flowStatus = resources.status.post
						break;

					default: //等待审核
						flowStatus = resources.status.unchecked
						break;
				}
				return flowStatus
			},
			handleClick(index) {
				const item = this.list[index]
				if ([4].includes(item.status)) return this.$u.toast("撤回的流程无法删除")
				if (item.status) {
					this.$u.toast("流程正在审核,请勿删除")
					this.list[index].show = false
					return
				}
				Delete(item.id).then(res => {
					this.$u.toast(res.msg)
					this.list.splice(index, 1)
					if (!this.list.length) this.mescroll.resetUpScroll()
				})
			},
			open(index) {
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if (index != idx) this.list[idx].show = false;
				})
			},
			search() {
				// 节流,避免输入过快多次请求
				this.searchTimer && clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.list = [];
					this.mescroll.resetUpScroll();
				}, 300)
			},
			goDetail(item) {


				uni.navigateTo({
					url: '/pages/q1/business/inspection/inspection?id=' + item.id + '&code=' + item.code
				})


			},
			getLableValue(value) {
				var lableValue = ''
				switch (value) {
					case 1:
						lableValue = '即将逾期'

						break;
					case 2:
						lableValue = '逾期'
						break;
					default:
						lableValue = '正常'
						break;
				}
				return lableValue
			},

		},



	}
</script>

<style lang="scss" scoped>
	@import "@/assets/scss/common.scss";

	page {
		background-color: #f0f2f6;
		height: 100%;
		/* #ifdef MP-ALIPAY */
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		/* #endif */
	}


	.flow-list .data-item {
		margin-bottom: 10px;
	}

	.w-100 {
		width: 100rpx;
	}

	.tag-class {
		position: absolute;
		right: 10px;
		top: 10px;
	}

	.flowLaunch-v {
		width: 100%;

		.flow-list-box {
			width: 95%;

			.item-left-top {
				display: flex;
				width: 100%;

				.common-lable {
					font-size: 24rpx;
					padding: 2rpx 8rpx;
					margin-right: 8rpx;
					border-radius: 8rpx;
					color: #ffffff;
					border: 1px solid #ffffff;
					background-color: #6dcc51;
				}

				.urgent-lable {
					color: #E6A23C;
					border: 1px solid #E6A23C;
					background-color: #fef6e5;
				}

				.important-lable {
					color: #F56C6C;
					border: 1px solid #F56C6C;
					background-color: #fee5e5;
				}

				.title {
					width: unset;
					flex: 1;
					min-width: 0;
				}
			}
		}
	}
</style>